use "${Data_Clean}/psmatched_panel_EV_utility.dta", clear
keep if match==0

/*Manually merge in Gas price data*/
/*Eventually, rerun Data_GIS_20200929.do to generate a version of psmatched_panel_EV_utility.dta that includes the GasPriceRadii.dta data*/

tempfile paneldata
save `paneldata', replace

	// gas data
	use "${Restricted_Data}/OPIS/zip_month_avg_retail_price.dta", clear
	
	ren mailingzip zipcode_primary
	
	/*New Addition in 0929*/
	merge 1:1 zipcode_primary year month using "${Restricted_Data}/OPIS/GasPriceRadii.dta"
	
	tempfile GasData
	save `GasData', replace
	
	use `paneldata', clear
	
merge n:1 zipcode_primary year month using `GasData', gen(gasmerge)
tab gasmerge

rename SellPrice1 P_BEV
rename SaleNumber1 Q_BEV
rename SellPrice2 P_PHEV
rename SaleNumber2 Q_PHEV
rename zipcode_primary zip
rename mean_income income
rename retailaverage P_gas

gen hybrid_share = hybrid_count / total_car_count
gen luxury_share = luxury_count / total_car_count
gen mud_hh_share = total_mud / total_hh


gen ActivePrice = HighestTierAvgRate
replace ActivePrice = AvgRateTier1 if (utility_name == "Pacific Gas & Electric Company" & (year == 2017 & month>=1))
replace ActivePrice = AvgRateTier1 if (utility_name == "San Diego Gas & Electric" & (year == 2017 | (year==2016 & month>=7)))
replace ActivePrice = AvgRateTier1 if (utility_name == "Southern California Edison" & (year == 2017 & month>=1))

tab modate utility_name if (utility_name == "Pacific Gas & Electric Company"  | utility_name == "San Diego Gas & Electric" |utility_name == "Southern California Edison"), sum(ActivePrice) nost nofreq

local kwhcutoff = 750
foreach kwhcutoff in 600 700 750 800 900 1000 {
gen KWH`kwhcutoff'MP = AvgRateTier0
forvalues x = 1 / 4 {	
	local y = `x' - 1
	replace KWH`kwhcutoff'MP = AvgRateTier`x' if (`kwhcutoff' > Tier`y'UsageAmt & `kwhcutoff' <= Tier`x'UsageAmt)
}
}

foreach kwhcutoff in 600 700 750 800 900 1000 {
gen KWH`kwhcutoff'AP = AvgRateTier0
replace KWH`kwhcutoff'AP = AvgRateTier0*Tier0UsageAmt + AvgRateTier1*(`kwhcutoff'-Tier0UsageAmt) if (`kwhcutoff' > Tier0UsageAmt & `kwhcutoff' <= Tier1UsageAmt)
replace KWH`kwhcutoff'AP = AvgRateTier0*Tier0UsageAmt + AvgRateTier1*Tier1UsageAmt +  AvgRateTier2*(`kwhcutoff'-Tier1UsageAmt) if (`kwhcutoff' > Tier1UsageAmt & `kwhcutoff' <= Tier2UsageAmt)
replace KWH`kwhcutoff'AP =  AvgRateTier0*Tier0UsageAmt + AvgRateTier1*Tier1UsageAmt + AvgRateTier2*Tier2UsageAmt + AvgRateTier3*(`kwhcutoff'-Tier2UsageAmt) if (`kwhcutoff' > Tier2UsageAmt & `kwhcutoff' <= Tier3UsageAmt)
replace KWH`kwhcutoff'AP = AvgRateTier0*Tier0UsageAmt + AvgRateTier1*Tier1UsageAmt + AvgRateTier2*Tier2UsageAmt + AvgRateTier3*Tier3UsageAmt + AvgRateTier4*(`kwhcutoff'-Tier3UsageAmt) if (`kwhcutoff' > Tier3UsageAmt & `kwhcutoff' <= Tier4UsageAmt)

replace KWH`kwhcutoff'AP=KWH`kwhcutoff'AP/`kwhcutoff'
}


gen dum_PGE = (utility_name == "Pacific Gas & Electric Company")
gen dum_SCE = (utility_name == "Southern California Edison")
gen dum_SDGE = (utility_name == "San Diego Gas & Electric")


rename HighestTierAvgRate p_e_high

rename ActivePrice p_e_active

local pop_var "population"
gen share_BEV = Q_BEV/(`pop_var'/10000)
gen share_PHEV = Q_PHEV/(`pop_var'/10000)

/*Put electricity in cents per KWH*/
foreach i of varlist p_e* KWH* {
	replace `i' = `i'*100
}



/*Put dollars and population in 000s*/
foreach i of varlist population income pop_density {
	replace `i' = `i'/1000
}

label var population "Population (000s)"
label var pop_density "Pop Density (000s ppl/sqm)"
label var income "Income (\\$000)"
label var P_gas "Gas Price (\\$/gal)"
label var p_e_active "Marg. Price (cents/kwh)"
label var p_e_high "Marg. Price - Top Tier (cents/kwh)"

label var fuel_economy_mean "Mean Fuel Econ (mpg, 2013)"
label var hybrid_share "Hybrid Fleet Share (\%, 2013)"
label var luxury_share "Luxury Fleet Share (\%, 2013)"
label var mud_hh_share "MUD HH share (\%, 2013)"


/*Put gasoline in cents per gallon*/
foreach i of varlist P_gas invdist* obswgt* simpleave* {
	replace `i' = `i'*100
label var P_gas "Gas Price (cpg)"
}

save "${Data_Clean}/panel_regdata.dta", replace